<template>
  <div class="tags">
    <ul class="current">
      <li
        v-for="(tag, index) in tagList"
        :key="tag.id"
        :class="{ selected: value.indexOf(tag) === 0 }"
        @click="toggle(tag)"
      >
        <div class="icon-wrapper">
          <Icon :name="index > 6 ? 'ordinary' : defaultTagList[index].name" />
        </div>
        {{ tag.name }}
      </li>
      <li>
        <div class="icon-wrapper" @click="createTag">
          <Icon name="add" />
        </div>
        新增
      </li>
    </ul>
  </div>
</template>

<script lang="ts">
import { Component, Prop } from 'vue-property-decorator'
import { mixins } from 'vue-class-component'
import TagHelper from '@/mixins/TagHelper'
import defaultTagList from '@/constants/defaultTagList'

@Component
export default class Tags extends mixins(TagHelper) {
  defaultTagList = defaultTagList
  @Prop() readonly value!: string[]
  get tagList() {
    return this.$store.state.tagList
  }

  created() {
    this.$store.commit('fetchTags')
    this.value.push(this.tagList[0])
  }

  toggle(tag: string) {
    const index = this.value.indexOf(tag)
    if (index === 0) {
      this.value.splice(index, 1)
    } else {
      this.value.splice(0, 1)
      this.value.push(tag)
    }
    this.$emit('update:value', this.value)
  }
}
</script>

<style lang="scss" scoped>
.tags {
  background: white;
  font-size: 14px;
  padding: 16px;
  flex-grow: 1;
  display: flex;
  flex-direction: column-reverse;
  > .current {
    display: flex;
    flex-wrap: wrap;
    align-content: center;
    justify-content: center;
    > li {
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      width: 20%;
      padding: 0 16px;
      margin-right: 12px;
      margin-top: 4px;
      > .icon-wrapper {
        width: 45px;
        height: 45px;
        padding: 5px;
        border-radius: 50%;
        background: #eeeeee;
        > .icon {
          width: 30px;
          height: 30px;
        }
      }
      &.selected {
        .icon-wrapper {
          background: #212121;
          > .icon {
            color: white;
          }
        }
      }
    }
  }
  > .new {
    padding-top: 16px;
    button {
      background: transparent;
      border: none;
      color: #999;
      border-bottom: 1px solid;
      padding: 0 4px;
    }
  }
}
</style>
